<?php

namespace Zms\Sms\Drive;

use Zms\Sms\Drive\Config\Rydconfig;
use Zms\Sms\SmsResult;
use Zms\Request\Http;
use Zms\Unit\Json;
use Zms\Unit\Str;

/**
 * 融易达短信
 * @link http://47.96.186.52:9998/help/sign-tool.html
 */
class Rydsms extends SmsBasics
{
    const SEND_SMS_API = '/yktsms/send';//发送任意短信api

    const SEND_SMS_TMP_API = '/yktsms/send';//发送模板短信api

    /**
     * 获取短信配置
     * @return Rydconfig
     */
    protected function getConfig(): Rydconfig
    {
        return parent::getConfig(); // TODO: Change the autogenerated stub
    }


    public function getBalance(): int
    {
        return 0;//不支持查询
    }

    /**
     * @describe 发送模板短信
     * @doc https://doc.zthysms.com/web/?#/1/13
     * @user zlf <zms-pro@qq.com> 2025/3/19
     * @param string $phone
     * @param string $template_key
     * @param array $data
     * @return SmsResult
     * @throws \Exception
     */
    public function send(string $phone, string $template_key, array $data = []): SmsResult
    {
        $conf = $this->getConfig();

        $content = $this->renderContent($conf->template, $template_key, array_merge($data, ['signature' => $conf->signature]));
        $postData = [
            'appid' => $conf->appid,
            'mobile' => $phone,
            'msg' => urlencode($content),
            'sign' => $this->getSign($conf->appid, $conf->appkey, $phone, $content),
        ];
        $request = Http::instance()->setHeader('Content-Type', 'application/json; charset=utf-8')->execute($conf->host . self::SEND_SMS_TMP_API, $postData);
        if ($request->getCode() === 200) {
            $body = $request->getBody();
            $res = explode(',', $body);
            $code = intval($res[0]);
            if ($code) {
                return new SmsResult(['phone' => $phone, 'content' => $content, 'status' => false, 'driver' => static::class, 'template_key' => $template_key, 'request' => $body]);
            }
            return new SmsResult(['phone' => $phone, 'content' => $content, 'status' => true, 'driver' => static::class, 'template_key' => $template_key, 'request' => $body]);
        }
        return new SmsResult(['phone' => $phone, 'content' => $content, 'status' => false, 'driver' => static::class, 'template_key' => $template_key, 'request' => $request]);
    }


    /**
     * @describe 发送任意短信
     * @doc https://doc.zthysms.com/web/?#/1/14
     * @user zlf <zms-pro@qq.com> 2025/3/19
     * @param string $phone
     * @param string $template_key
     * @param array $data
     * @return SmsResult
     * @throws \Exception
     */
    public function sendBody(string $phone, string $content, array $data = []): SmsResult
    {
        $conf = $this->getConfig();
        $content = Str::render($content, array_merge($data, ['signature' => $conf->signature]));
        $postData = [
            'appid' => $conf->appid,
            'mobile' => $phone,
            'msg' => urlencode($content),
            'sign' => $this->getSign($conf->appid, $conf->appkey, $phone, $content),
        ];
        $request = Http::instance()->setHeader('Content-Type', 'application/json; charset=utf-8')->execute($conf->host . self::SEND_SMS_TMP_API, $postData);
        if ($request->getCode() === 200) {
            $body = $request->getBody();
            $res = explode(',', $body);
            $code = intval($res[0]);
            if ($code) {
                return new SmsResult(['phone' => $phone, 'content' => $content, 'status' => false, 'driver' => static::class, 'template_key' => $template_key, 'request' => $body]);
            }
            return new SmsResult(['phone' => $phone, 'content' => $content, 'status' => true, 'driver' => static::class, 'template_key' => $template_key, 'request' => $body]);
        }
        return new SmsResult(['phone' => $phone, 'content' => $content, 'status' => false, 'driver' => static::class, 'template_key' => $template_key, 'request' => $request]);
    }


    /**
     * 获取模板ID
     * @param string $template_key
     * @param array $template
     * @return string
     */
    private function getTemplateCode(string $template_key, array $template): string
    {
        foreach ($template as $item) {
            if ($item['KEY'] === $template_key) {
                return $item['TEMPLATE_ID'];
            }
        }
        return '';
    }


    private function getSign(string $appid, string $appkey, string $phone, string $content)
    {
        return md5("{$appid}{$phone}{$content}{$appkey}");
    }
}